﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Windows.Forms;
using System.Linq;

namespace Company.vsEnv
{
    public partial class MyControl : UserControl
    {
        public MyControl()
        {
            InitializeComponent();
        }

        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if (keyData == (Keys.Control | Keys.C))
            {
                textBox1.Copy();
                return true;
            }
            else if (keyData == (Keys.Control | Keys.V))
            {
                textBox1.Paste();
                return true;
            }
            else if (keyData == (Keys.Control | Keys.X))
            {
                textBox1.Cut();
                return true;
            }
            else if (keyData == (Keys.Control | Keys.A))
            {
                textBox1.SelectAll();
                return true;
            }
            else if (keyData == (Keys.Control | Keys.Z))
            {
                textBox1.Undo();
                return true;
            }


            return base.ProcessCmdKey(ref msg, keyData);
        }

        private void MyControl_Load(object sender, EventArgs e)
        {

        }

        private void MyControl_Leave(object sender, EventArgs e)
        {

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        public string VariableText
        {
            get { return textBox1.Text; }

            set
            {
                textBox1.Text = value;
            }

        }

        private void textBox1_Validating(object sender, System.ComponentModel.CancelEventArgs e)
        {
            Dictionary<string, Dictionary<string, string>> temp;
            string message;
            if (!Helper.TryParseIni(textBox1.Text, out temp, out message))
            {
                MessageBox.Show(message, "Invalid configuration", MessageBoxButtons.OK, MessageBoxIcon.Error);
                e.Cancel = true;
            }
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            var a = textBox1.AcceptsReturn;
        }

        private void textBox1_Validated(object sender, EventArgs e)
        {

        }

        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                e.Handled = true;
                textBox1.SelectedText = "\r\n";
            }
        }

        private void textBox1_Leave(object sender, EventArgs e)
        {

        }

        private void textBox1_Enter(object sender, EventArgs e)
        {

        }

        private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {

        }

        private void btnViewCurrent_Click(object sender, EventArgs e)
        {
            StringBuilder sb = new StringBuilder();
            var allVariable = Environment.GetEnvironmentVariables();
            foreach (var variable in allVariable.Keys.OfType<string>().OrderBy(x=>x))
            {
                sb.AppendLine(variable + "=" + allVariable[variable]);
            }
            string temp = Path.GetTempFileName()+".txt";
            File.WriteAllText(temp, sb.ToString());
            Process.Start(temp);
        }
    }
}
